package com.acwing.partition9;

import java.io.*;

/**
 * @author `RKC`
 * @date 2021/12/4 21:38
 */
public class AC878线性同余方程 {

    private static int x, y;

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        //a×x≡b(mod m)：ax除以m的余数是b
        while (n-- > 0) {
            s = reader.readLine().split(" ");
            int a = Integer.parseInt(s[0]), b = Integer.parseInt(s[1]), m = Integer.parseInt(s[2]);
            int d = extendGcd(a, m);
            if (b % d != 0) writer.write("impossible\n");
            else writer.write(((long) x * (b / d) % m) + "\n");
        }
        writer.flush();
    }

    private static int extendGcd(int a, int b) {
        if (b == 0) {
            //当b等于0时，ax+by=gcd(a,b)，ax=a，x只能等于1
            x = 1;
            y = 0;
            return a;
        }
        int d = extendGcd(b, a % b), temp = x;
        x = y;
        y = temp - a / b * y;
        return d;
    }
}
